import { createRouter, createWebHistory, RouteOption } from "vue-router";
/* Layout */
import Layout from "@/layout/index.vue";
import { getToken } from "@/utils/auth";
import { useUserStore } from "@/stores/modules/user";
import useAppStore from "@/stores/modules/app";
import { isEmpty } from "@/utils";
import usePermissionStore from "@/stores/modules/permission";

// 需要登录信息的白名单路由
const routeWhiteList = ["/401", "/403"];
// 不需要登录信息的路由
const routeWhiteListWithoutUserInfo = [
  "/login",
  "/404",
  "/500",
  "/error",
  "/personalCenter",
];

export const constantRoutes: RouteOption[] = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/home/index.vue"),
        name: "Index",
        meta: { title: "首页", icon: "dashboard", affix: true },
      },
    ],
  },
  {
    path: "/sys",
    component: Layout,
    redirect: "/index",
    hidden: true,
    children: [
      {
        path: "personal",
        name: "personal",
        component: () => import("@/views/sys/personal/index.vue"),
        meta: {
          title: "个人中心",
          icon: "dashboard",
          affix: false,
          keepAlive: false,
        },
      },
      {
        path: "config",
        name: "sysConfig",
        component: () => import("@/views/sys/config/index.vue"),
        meta: { title: "系统参数", icon: "dashboard", affix: false },
        hidden: true,
      },
      {
        path: "dict",
        name: "sysDict",
        component: () => import("@/views/sys/dict/index.vue"),
        meta: { title: "系统字典", icon: "dashboard", affix: false },
        hidden: true,
      },
      {
        path: "menu",
        name: "sysMenu",
        component: () => import("@/views/sys/menu/index.vue"),
        meta: { title: "系统菜单", icon: "dashboard", affix: false },
        hidden: true,
      },
      {
        path: "orgMember",
        name: "sysOrgMember",
        component: () => import("@/views/sys/orgMember/index.vue"),
        meta: { title: "机构人员管理", icon: "dashboard", affix: false },
        hidden: true,
      },
      {
        path: "role",
        name: "sysRole",
        component: () => import("@/views/sys/role/index.vue"),
        meta: { title: "角色管理", icon: "dashboard", affix: false },
        hidden: true,
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue"),
    meta: { title: "登录", icon: "dashboard" },
    hidden: true,
  },
];
// 动态路由，基于用户权限动态去加载
export const dynamicRoutes: RouteOption[] = [];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
});

router.beforeEach(async (to, _, next) => {
  document.title = `${to.meta.title || to.name} `;
  const accessToken = getToken();
  // 不需要登录信息的白名单菜单
  if (
    to.meta?.whitelist === true ||
    routeWhiteListWithoutUserInfo.includes(to.path)
  ) {
    return next();
  }

  // 没有登录
  if (accessToken) {
    //当前用户信息是否存在
    if (isEmpty(useUserStore().userInfo)) {
      try {
        await useAppStore().initDict();
        await useUserStore().getUserInfo();
        await usePermissionStore().getAllMenus();
        await usePermissionStore().getPermissions();
      } catch (error) {
        console.log(error);
        next("/500");
      }
    }
    return next();
  } else {
    return next(`/login?redirect=${to.fullPath}`);
  }
  // 默认不带 id 的路由认为不需要鉴权
  // if (routeWhiteList.includes(to.path) || !to.meta.id) {
  //   return next(needRedirct ? { ...to } : undefined);
  // }

  // 默认下一步
  next();
});

export default router;
